home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gcctest / tests05.zoo / tstrtoul.c < prev    next >
C/C++ Source or Header  |  1993-03-02  |  3KB  |  118 lines

  1. #include <stdio.h>
  2. #include <errno.h>
  3. extern int errno;
  4.  
  5. #define CONVRT strtoul
  6.  
  7.  
  8.  
  9. #if __STDC__
  10. #include <stdlib.h>
  11. #include <limits.h>
  12. #else
  13. #define const /* */
  14. extern unsigned long CONVRT();
  15. #define LONG_MAX 2147483647L
  16. #define LONG_MIN (-2147483648L)
  17. #define ULONG_MAX (4294967295U)
  18. #endif
  19. #ifndef OK
  20. #define OK 0
  21. #endif
  22.  
  23. struct test {
  24.   const char    *string;    /* the string         */
  25.   unsigned long  val;        /* expected result     */
  26.   int        err;        /* expected errno     */
  27.   int        endp;        /* index of expected end ptr    */
  28. } tests [] = {
  29.   { "0",        0L,        OK,      1     },
  30.   { "-12345",        -12345L,    OK,      6     },
  31.   { "12345",        12345L,        OK,      5     },
  32.   { "-0x12345",        -0x12345L,    OK,      8     },
  33.   { "0x12345",        0x12345L,    OK,      7     },
  34.   { "-012345",        -012345L,    OK,      7     },
  35.   { "012345",        012345L,    OK,      6     },
  36.   { "10]",              10L,        OK,      2     },
  37.                              
  38.   { "2147483645L",    2147483645L,    OK,      10     },
  39.   { "2147483646L",    2147483646L,    OK,      10     },
  40.   { "2147483647L",    2147483647L,    OK,      10     },
  41.   { "2147483648L",    2147483648L,    OK,     10     },
  42.   { "2147483649L",    2147483649L,    OK,     10     },
  43.                              
  44.   { "-2147483646L",    2147483650L,    OK,      11     },
  45.   { "-2147483647L",    2147483649L,    OK,      11     },
  46.   { "-2147483648L",    2147483648L,    OK,      11     },
  47.   { "-2147483649L",    2147483647L,    OK,     11     },
  48.   { "-2147484648L",    2147482648L,    OK,     11     },
  49.                              
  50.   { "       567L",    567L,        OK,      10     },
  51.   { "\t      567 ",    567L,        OK,      10     },
  52.   { "      -567]",     -567L,        OK,      10     },
  53.                              
  54.   { "ZZZZZZ",          0L,        OK,      0     },
  55.   { "0xGZZZZZ",          0L,        OK,      1     },
  56.   { "08ZZZZZZ",          0L,        OK,      1     },
  57.   { "+0x1ZZZZZZ",      1L,        OK,      4     },
  58.   { " -07188888",      -071L,        OK,      5     },
  59.                              
  60.   { " +017777777777+",    LONG_MAX,    OK,      14     },
  61.   { " +027777777777+",    3221225471,    OK,      14     },
  62.   { " -017777777777+",    LONG_MIN+1,    OK,      14     },
  63.   { " -020000000000+",    LONG_MIN,    OK,      14     },
  64.   { " -037777777777+",    1,        OK,     14     },
  65.   { "-037777777777+",    1,        OK,      13     },
  66.   { "037777777777+",    ULONG_MAX,    OK,     12     },
  67.                              
  68.   { ""            ,          0,       OK,      0     },
  69.   { "\t\t"        ,          0,       OK,      0     },
  70.   { "+"            ,          0,       OK,      0     },
  71.   { "-"            ,          0,       OK,      0     },
  72.   { NULL,               0,    OK,      0     }
  73. };
  74.  
  75. void fail(i, test, res, err, endp)
  76. int i;
  77. struct test *test;
  78. long res;
  79. int err;
  80. char *endp;
  81. {
  82.     printf("Test %d (\"%s\") failed\n", i, test->string);
  83.     printf("\tExpecting:\t%10lu\t%3d\t%x\n", test->val, test->err,
  84.  &(test->string[(test->endp)]));
  85.     printf("\tGot:\t\t%10lu\t%3d\t%x\n", res, err, endp);
  86. }
  87.  
  88. int main()
  89. {
  90.     int i, errs = 0;
  91.     unsigned long res;
  92.     char *endp;
  93.  
  94.     for(i = 0; tests[i].string; i++)
  95.     {
  96.     errno = OK;
  97.     res = CONVRT(tests[i].string, &endp, 0);
  98.     if(res != tests[i].val)
  99.     {
  100.        fail(i, &tests[i], res, errno, endp);
  101.        errs++;
  102.     }
  103.     else if(tests[i].err != errno)
  104.     {
  105.        fail(i, &tests[i], res, errno, endp);
  106.        errs++;
  107.     }
  108.     else if( &(tests[i].string[(tests[i].endp)]) != endp)
  109.     {
  110.        fail(i, &tests[i], res, errno, endp);
  111.        errs++;
  112.     }
  113.     }
  114.  
  115.     if (errs) printf("%d error(s)\n", errs);
  116.     return errs ?  EXIT_FAILURE : EXIT_SUCCESS;
  117. }
  118.